package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.work.domain.WorkDeclarer;
import com.ruoyi.work.domain.WorkStage;
import com.ruoyi.work.service.IWorkDeclarerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: MMDZ
 * @Desc: TODO 人员项目配置
 */
@RestController
@RequestMapping("/work/declarer")
public class WorkDeclarerController extends BaseController {

    @Autowired
    private IWorkDeclarerService workDeclarerService;

    /**
     * 人员项目配置列表
     */
    @PreAuthorize("@ss.hasPermi('work:declarer:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkDeclarer declarer) {
        startPage();
        List<WorkDeclarer> list = workDeclarerService.selectWorkDeclarerList(declarer);
        return getDataTable(list);
    }

    /**
     * 详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:declarer:query')")
    @GetMapping(value = "/info/{declarerId}")
    public AjaxResult getInfo(@PathVariable("declarerId") Long declarerId) {
        return AjaxResult.success(workDeclarerService.selectDeclarerById(declarerId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('work:declarer:add')")
    @Log(title = "新增人员项目配置", businessType = BusinessType.INSERT)
    @GetMapping(value = "/{userIds}")
    public AjaxResult add(@PathVariable Long[] userIds) {
        return toAjax(workDeclarerService.insertWorkDeclarerByIds(userIds));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('work:declarer:edit')")
    @Log(title = "修改人员项目配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkDeclarer workDeclarer) {
        if (Objects.isNull(workDeclarer.getPrincipalId()))
            throw new ServiceException("责任人为空，请选择责任人", HttpStatus.ERROR);
        return toAjax(workDeclarerService.updateDeclarer(workDeclarer));
    }

    /**
     * 移除
     */
    @PreAuthorize("@ss.hasPermi('work:declarer:remove')")
    @Log(title = "移除人员项目配置", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{declarerId}")
    public AjaxResult delete(@PathVariable Long declarerId) {
        return AjaxResult.success(workDeclarerService.deleteWorkDeclarerById(declarerId));
    }

    /**
     * 选择框列表（没有加入的人员项目）
     */
    @GetMapping("/optionselect")
    public TableDataInfo optionselect(WorkDeclarer declarer) {
        startPage();
        List<WorkDeclarer> list = workDeclarerService.selectWorkDeclarerUnselected(declarer);
        return getDataTable(list);
    }
}
